package com.punai.externalccb.bo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.util.StringUtil;
import com.punai.externalccb.domain.EcoBidnode;
import com.punai.externalccb.domain.EcoCbfeeitem;
import com.punai.prodatalibrary.constants.ProjectLibConstants;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class EcoCbfeeitemBo extends EcoCbfeeitem {

    private BigDecimal taxyke;//盈亏额(含税)
    private BigDecimal taxykl;//盈亏率(含税)

    private BigDecimal xxsje;//销项税金额
    private BigDecimal lwfb;//劳务分包
    private BigDecimal zyfb;//专业分包
    private BigDecimal clf;//材料费
    private BigDecimal zzclf;//周转材料费
    private BigDecimal jxf;//机械费
    private BigDecimal aqwmsgf;//安全文明施工费
    private BigDecimal lsssf;//临时设施费
    private BigDecimal qtcsf;//其他措施费
    private BigDecimal glf;//管理费
    private BigDecimal jsxhj;//进项税合计
    private BigDecimal je;//金额
    private BigDecimal fs;//负税
    private List<EcoCbfeeitemBo> children = new ArrayList<>();

    public static EcoCbfeeitemBo convert(EcoCbfeeitem old){
        EcoCbfeeitemBo bo = new EcoCbfeeitemBo();
        if (old != null){
//            //盈亏额(含税) = ysTotal - targetTaxTotal
//            BigDecimal ystotal = old.getYstotal();
//            BigDecimal targettaxtotal = old.getTargettaxtotal();
//            BigDecimal taxyke = null;
//            BigDecimal taxykl = null;
//            //盈亏率(含税) = (ysTotal - targetTaxTotal) / ysTotal
//            if (ystotal != null && targettaxtotal != null){
//                taxyke = ystotal.subtract(targettaxtotal);
//                if (ystotal.compareTo(BigDecimal.ZERO) == 0){
//                    taxykl = new BigDecimal("0.00");
//                }else {
//                    taxykl = taxyke.divide(ystotal,4,BigDecimal.ROUND_HALF_UP);
//                }
//            }
//            bo.setTaxyke(taxyke);
//            bo.setTaxykl(taxykl);
            BeanUtil.copyProperties(old,bo);
            if (StringUtil.isNotEmpty(old.getSfdk())){
                bo.setSfdk(old.getSfdk().equals("Y") ? "是" : "否");
            }
        }
        return bo;
    }

    public static EcoCbfeeitemBo convertBidnode(EcoBidnode old,List<EcoCbfeeitem> list){
        EcoCbfeeitemBo bo = new EcoCbfeeitemBo();
        if (old != null){
            bo.setName(old.getNodename());
            if (CollUtil.isNotEmpty(list)){
                //BigDecimal ystotal = list.stream().filter(v -> v.getYstotal() != null).map(EcoCbfeeitem::getYstotal).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(3,BigDecimal.ROUND_HALF_UP);//预算收入
                BigDecimal ystotal = old.getAmount();//预算收入
                BigDecimal xxsje = old.getSjrate();//销项税金额
                bo.setYstotal(ystotal);
                bo.setXxsje(xxsje);
                Map<Integer, List<EcoCbfeeitem>> map = list.stream().collect(Collectors.groupingBy(EcoCbfeeitem::getFeetype, Collectors.toList()));
                bo.setLwfb(add(ProjectLibConstants.Cost_TYPE.LWFB.getCode(),map));
                bo.setZyfb(add(ProjectLibConstants.Cost_TYPE.ZYFB.getCode(),map));
                bo.setClf(add(ProjectLibConstants.Cost_TYPE.CLF.getCode(),map));
                bo.setZzclf(add(ProjectLibConstants.Cost_TYPE.ZZCLF.getCode(),map));
                bo.setJxf(add(ProjectLibConstants.Cost_TYPE.JXF.getCode(),map));
                bo.setAqwmsgf(add(ProjectLibConstants.Cost_TYPE.AQWMSGF.getCode(),map));
                bo.setLsssf(add(ProjectLibConstants.Cost_TYPE.LSSSF.getCode(),map));
                bo.setQtcsf(add(ProjectLibConstants.Cost_TYPE.QTCSF.getCode(),map));
                bo.setGlf(add(ProjectLibConstants.Cost_TYPE.GLF.getCode(),map));
                BigDecimal jxshj = list.stream().filter(v -> v.getTargetsjtotal() != null).map(EcoCbfeeitem::getTargetsjtotal).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(3,BigDecimal.ROUND_HALF_UP);
                bo.setJsxhj(jxshj);//进项税合计
                BigDecimal je = null;//金额
                BigDecimal fs = null;//负税
                if (xxsje!=null&&jxshj!=null){
                    je = xxsje.subtract(jxshj);
                    if (ystotal.compareTo(BigDecimal.ZERO) == 0){
                        fs = new BigDecimal("0.000");
                    }else {
                        fs = je.divide(ystotal,4,BigDecimal.ROUND_HALF_UP);
                    }
                }
                bo.setJe(je);
                bo.setFs(fs);
            }
        }
        return bo;
    }

    private static BigDecimal add(Integer feeType,Map<Integer, List<EcoCbfeeitem>> map){
        BigDecimal res = null;
        if (CollUtil.isNotEmpty(map) && feeType != null){
            List<EcoCbfeeitem> ecoCbfeeitems = map.get(feeType);
            if (CollUtil.isEmpty(ecoCbfeeitems)) return res;
            res = ecoCbfeeitems.stream().filter(v -> v.getTargetsjtotal() != null).map(EcoCbfeeitem::getTargetsjtotal).reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        return res!=null ? res.setScale(3,BigDecimal.ROUND_HALF_UP) : res;
    }

    public BigDecimal getTaxyke() {
        return taxyke;
    }

    public void setTaxyke(BigDecimal taxyke) {
        this.taxyke = taxyke;
    }

    public BigDecimal getTaxykl() {
        return taxykl;
    }

    public void setTaxykl(BigDecimal taxykl) {
        this.taxykl = taxykl;
    }

    public BigDecimal getXxsje() {
        return xxsje;
    }

    public void setXxsje(BigDecimal xxsje) {
        this.xxsje = xxsje;
    }

    public BigDecimal getLwfb() {
        return lwfb;
    }

    public void setLwfb(BigDecimal lwfb) {
        this.lwfb = lwfb;
    }

    public BigDecimal getZyfb() {
        return zyfb;
    }

    public void setZyfb(BigDecimal zyfb) {
        this.zyfb = zyfb;
    }

    public BigDecimal getClf() {
        return clf;
    }

    public void setClf(BigDecimal clf) {
        this.clf = clf;
    }

    public BigDecimal getZzclf() {
        return zzclf;
    }

    public void setZzclf(BigDecimal zzclf) {
        this.zzclf = zzclf;
    }

    public BigDecimal getJxf() {
        return jxf;
    }

    public void setJxf(BigDecimal jxf) {
        this.jxf = jxf;
    }

    public BigDecimal getAqwmsgf() {
        return aqwmsgf;
    }

    public void setAqwmsgf(BigDecimal aqwmsgf) {
        this.aqwmsgf = aqwmsgf;
    }

    public BigDecimal getLsssf() {
        return lsssf;
    }

    public void setLsssf(BigDecimal lsssf) {
        this.lsssf = lsssf;
    }

    public BigDecimal getQtcsf() {
        return qtcsf;
    }

    public void setQtcsf(BigDecimal qtcsf) {
        this.qtcsf = qtcsf;
    }

    public BigDecimal getGlf() {
        return glf;
    }

    public void setGlf(BigDecimal glf) {
        this.glf = glf;
    }

    public BigDecimal getJsxhj() {
        return jsxhj;
    }

    public void setJsxhj(BigDecimal jsxhj) {
        this.jsxhj = jsxhj;
    }

    public BigDecimal getJe() {
        return je;
    }

    public void setJe(BigDecimal je) {
        this.je = je;
    }

    public BigDecimal getFs() {
        return fs;
    }

    public void setFs(BigDecimal fs) {
        this.fs = fs;
    }

    public List<EcoCbfeeitemBo> getChildren() {
        return children;
    }

    public void setChildren(List<EcoCbfeeitemBo> children) {
        this.children = children;
    }
}
